package com.zh.note.huawei.twohundred.篮球比赛;

import java.util.*;

/**
 * https://dream.blog.csdn.net/article/details/129699115
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // String line = sc.nextLine();
        String line = "10 9 8 7 6 5 4 3 2 1";
        int sum = 0;
        String[] strs = line.split(" ");
        int[] nums = new int[strs.length];
        // 求出总和
        for (int i = 0; i < strs.length; i++) {
            nums[i] = Integer.parseInt(strs[i]);
            sum += nums[i];
        }
        List<List<Integer>> res = new ArrayList<>();
        Deque<Integer> path = new LinkedList<>();
        dfs(nums, 0, path, res);
        // System.out.println(res);

        int minSum = Integer.MAX_VALUE;
        for (List<Integer> list : res) {
            int count = list.stream().mapToInt(Integer::intValue).sum(); // 统计每个组合之和
            // 一对count 2对 sum - count 差为：sum-2count
            int abs = Math.abs(sum - 2 * count);
            if (abs < minSum) {
                minSum = abs;
            }
        }
        System.out.println(minSum);
    }

    // 取5个数的组合
    private static void dfs(int[] nums, int startIndex, Deque<Integer> path, List<List<Integer>> res) {
        if (path.size() == 5) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex; i < nums.length; i++) {
            path.add(nums[i]);
            dfs(nums, i + 1, path, res);
            path.removeLast();
        }
    }
}
